package com.common.app.data.repository

import com.common.app.data.api.ApiConstant.getBaseUrl
import com.common.app.data.bean.circle.CircleDetailModel
import com.common.app.data.bean.circle.CircleListModel
import com.common.app.data.bean.circle.CircleMemberListModel
import com.common.app.data.bean.circle.CircleTaskModel
import com.common.app.data.bean.circle.CirclesClickLikeModel
import com.common.app.data.bean.circle.CirclesMedalBadgesBean
import com.common.app.data.bean.circle.CirclesMedalModel
import com.common.app.data.bean.circle.CirclesNotificationsModel
import com.common.app.data.bean.circle.CirclesRankModel
import com.common.base.data.CommonBean
import com.common.base.data.ListCommon
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import rxhttp.toAwait
import rxhttp.wrapper.param.RxHttp

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/07/24
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
object CircleApi {

    //获取圈子列表
    private val GET_CIRCLES_LIST = getBaseUrl() + "api/v1/ocell-ring/circles/list"
    //获取圈子详情
    private val GET_CIRCLE_DETAIL = getBaseUrl() + "api/v1/ocell-ring/circles/detail"
    //获取圈子成员
    private val GET_CIRCLE_MEMBERS_LIST = getBaseUrl() + "api/v1/ocell-ring/circles/members/list"
    //获取圈子任务
    private val GET_CIRCLE_TASK = getBaseUrl() + "api/v1/ocell-ring/circles/tasks"
    //获取圈子任务排行榜
    private val GET_CIRCLE_TASK_RANKINGS = getBaseUrl() + "api/v1/ocell-ring/circles/rankings"
    //创建圈子
    private val CREATE_CIRCLE = getBaseUrl() + "api/v1/ocell-ring/circles"
    //更新圈子
    private val UP_CIRCLE_INFO = getBaseUrl() + "api/v1/ocell-ring/circles/update"
    //创建圈子任务
    private val CREATE_CIRCLE_TASK = getBaseUrl() + "api/v1/ocell-ring/circles/tasks/config"
    //删除圈子成员
    private val CREATE_DEL_MEMBERS = getBaseUrl() + "api/v1/ocell-ring/circles/members/remove"
    //删除圈子
    private val DEL_CIRCLE = getBaseUrl() + "api/v1/ocell-ring/circles/delete"
    //加入圈子
    private val JOIN_CIRCLE = getBaseUrl() + "api/v1/ocell-ring/circles/join"
    //退出圈子
    private val EXIT_CIRCLE = getBaseUrl() + "api/v1/ocell-ring/circles/leave"
    //勋章列表
    val MEDAL_LIST = getBaseUrl() + "api/v1/smart-ring/badge"
    //对外展示勋章列表
    private val OUT_MEDAL_LIST = getBaseUrl() + "api/v1/ocell-ring/badge/display-list"
    //编辑外展示勋章状态
    private val CHANGE_OUT_MEDAL = getBaseUrl() + "api/v1/ocell-ring/badge/set-display"
    //获取圈子消息通知
    private val GET_CIRCLE_NOTIFICATIONS = getBaseUrl() + "api/v1/ocell-ring/notifications/circle"
    //点赞
    val CLICK_LIKE = getBaseUrl() + "api/v1/ocell-ring/circles/like"

    suspend fun getCirclesList(type:String) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(GET_CIRCLES_LIST)
                .add("type",type)
                .toAwait<ListCommon<CircleListModel>>()
                .await()
        }

    suspend fun getCirclesDetail(circleId:Int) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(GET_CIRCLE_DETAIL)
                .add("circle_id",circleId)
                .toAwait<CommonBean<CircleDetailModel>>()
                .await()
        }

    suspend fun getCirclesMembersList(circleId:Int) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(GET_CIRCLE_MEMBERS_LIST)
                .add("circle_id",circleId)
                .toAwait<CommonBean<CircleMemberListModel>>()
                .await()
        }

    suspend fun requestCreateCircles(id:Int,name:String,imgId:Long,isPublic:Boolean = false,location:String,description:String) =
        withContext(Dispatchers.IO) {
            val response = RxHttp.postJson(if(id>0) UP_CIRCLE_INFO else CREATE_CIRCLE)
            if(id>0) response.add("id",id)
            if(id>0) response.add("location",location)
            if(imgId>0) response.add("image_id",imgId)
            response.add("is_public",isPublic)
                .add("name",name)
                .add("description",description)
                .toAwait<CommonBean<Any>>()
                .await()
        }

    suspend fun getCirclesTask(circleId:Int) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(GET_CIRCLE_TASK)
                .add("circle_id",circleId)
                .toAwait<CommonBean<CircleTaskModel>>()
                .await()
        }

    suspend fun getCirclesTaskRank(circleId:Int,type:String) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(GET_CIRCLE_TASK_RANKINGS)
                .add("circle_id",circleId)
                .add("type",type)
                .toAwait<CommonBean<CirclesRankModel>>()
                .await()
        }

    suspend fun requestDelCirclesMembers(circleId:Int,memberId:Int) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(CREATE_DEL_MEMBERS)
                .add("circle_id",circleId)
                .add("user_id",memberId)
                .toAwait<CommonBean<Any>>()
                .await()
        }
    suspend fun requestConfigCirclesTask(circleId:Int,cycle:Int,daysRequired:Int,distanceRequired:Double,startTime:Long,taskType:String,) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(CREATE_CIRCLE_TASK)
                .add("circle_id",circleId)
                .add("cycle",cycle)
                .add("days_required",daysRequired)
                .add("distance_required",distanceRequired)
                .add("start_time",startTime)
                .add("task_type",taskType)
                .toAwait<CommonBean<Any>>()
                .await()
        }
    suspend fun requestDelCircles(circleId:Int) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(DEL_CIRCLE)
                .add("circle_id",circleId)
                .toAwait<CommonBean<Any>>()
                .await()
        }
    suspend fun requestJoinCircles(circleId:Int) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(JOIN_CIRCLE)
                .add("circle_id",circleId)
                .toAwait<CommonBean<Any>>()
                .await()
        }
    suspend fun requestExitCircles(circleId:Int) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(EXIT_CIRCLE)
                .add("circle_id",circleId)
                .toAwait<CommonBean<Any>>()
                .await()
        }

    suspend fun getMedalList(id:Int=0) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(MEDAL_LIST)
                .add("user_id",id)
                .toAwait<ListCommon<CirclesMedalModel>>()
                .await()
        }
    suspend fun getOutMedalList(id:Int=0) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(OUT_MEDAL_LIST)
                .add("user_id",id)
                .toAwait<ListCommon<CirclesMedalBadgesBean>>()
                .await()
        }
    suspend fun requestChangeOutMedal(id:Int=0,) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(CHANGE_OUT_MEDAL)
                .add("badge_id",id)
                .toAwait<CommonBean<Any>>()
                .await()
        }

    suspend fun requestClickLike(id:Int=0) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(CLICK_LIKE)
                .add("target_user_id",id)
                .toAwait<CommonBean<CirclesClickLikeModel>>()
                .await()
        }

    suspend fun getCirclesNotificationsList() =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(GET_CIRCLE_NOTIFICATIONS)
                .toAwait<ListCommon<CirclesNotificationsModel>>()
                .await()
        }

}